import "package:flutter/material.dart";
import 'package:login_demo/app_theme.dart';
import 'package:login_demo/login.dart';
import 'package:login_demo/register.dart';
import 'package:login_demo/widget/TextInput.dart';
import 'package:login_demo/widget/button.dart';
import 'package:oktoast/oktoast.dart';

import 'forgetPassword.dart';
// desc: Login
class SetNewPassword extends StatefulWidget {
  const SetNewPassword({Key key}) : super(key: key);

  @override
  _SetNewPasswordState createState() => _SetNewPasswordState();
}

class _SetNewPasswordState extends State<SetNewPassword> {

  TextEditingController smsController;
  TextEditingController newController;
  TextEditingController confirmController;

  @override
  void initState() {
    smsController = new TextEditingController();
    newController = new TextEditingController();
    confirmController = new TextEditingController();
    
    Future.delayed(Duration(seconds: 2), (){
      showToast('收到验证码：4321');
    });
    
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: AppTheme.background,
      body: Container(
        color: AppTheme.background,
        padding: EdgeInsets.only(left: 15, right: 15),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 40),
            Row(
              children: [
                GestureDetector(
                  onTap: (){
                    Navigator.pop(context);
                  },
                  child: Icon(Icons.arrow_back_ios_rounded),
                ),
              ],
            ),
            SizedBox(height: 40),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('设置密码', style: TextStyle(
                    fontSize: 16,
                    color: AppTheme.textBlack,
                    fontWeight: FontWeight.bold
                )),
              ],
            ),
            SizedBox(height: 30),
            Text('验证码', style: TextStyle(color: AppTheme.dark)),
            Padding(
              padding: EdgeInsets.only(left: 10, right: 10, top: 10),
              child: TextInput(
                icon: null,
                hintText: '请输入验证码',
                textEditingController: smsController,
                fillColor: true,
              ),
            ),
            SizedBox(height: 30),
            Text('新密码', style: TextStyle(color: AppTheme.dark)),
            Padding(
              padding: EdgeInsets.only(left: 10, right: 10, top: 10),
              child: TextInput(
                icon: Icon(Icons.lock),
                hintText: '请输入新密码',
                textEditingController: newController,
                fillColor: true,
              ),
            ),
            SizedBox(height: 15),
            Text('再输入一次', style: TextStyle(color: AppTheme.dark)),
            Padding(
              padding: EdgeInsets.only(left: 10, right: 10, top: 10),
              child: TextInput(
                icon: Icon(Icons.lock),
                hintText: '请确认密码',
                textEditingController: confirmController,
                fillColor: true,
              ),
            ),
            SizedBox(height: 80),
            Button(
              text: '确定',
              onTap: (){
                _confirm();
              },
            ),
          ],
        ),
      ),
    );
  }

  _confirm(){
    if(smsController.text == '') {
      showToast('验证码不可为空');
    } else if(newController.text == '') {
      showToast('设置密码不可为空');
    } else if( newController.text != confirmController.text) {
      showToast('两次输入的密码不一致');
    } else {
      _remind();
    }
  }

  _remind() {
    showDialog(
        context: context,
        builder: (BuildContext context){
          return Material(
            color: Colors.grey.withOpacity(0.2),
            child: GestureDetector(
              // onTap: (){ Navigator.pop(context); },
              child: Container(
                width: double.maxFinite,
                height: double.maxFinite,
                color: Colors.transparent,
                child: Align(
                  child: Container(
                    margin: EdgeInsets.only(left: 30, right: 30),
                    height: 180,
                    color: Colors.white,
                    child: Column(
                      children: [
                        Padding(
                          padding: EdgeInsets.only(top: 15, bottom: 15),
                          child: Text('温馨提示', style: TextStyle(
                              fontSize: 14,
                              color: AppTheme.textBlack,
                              fontWeight: FontWeight.bold
                          )),
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 15),
                          child: Text('密码设置成功，点击确定直接去登录吧！', style: TextStyle(
                            fontSize: 14,
                            color: AppTheme.textBlack,
                          )),
                        ),
                        Expanded(
                            child: SizedBox()
                        ),
                        GestureDetector(
                          onTap: (){
                            _gotoLogin();
                          },
                          child: Container(
                            padding: EdgeInsets.only(top: 15, bottom: 15),
                            decoration: BoxDecoration(
                                border: Border(
                                    top: BorderSide(color: AppTheme.hintGrey)
                                )
                            ),
                            child: Center(
                              child: Text('确定', style: TextStyle(fontSize: 16, color: AppTheme.textBlack),),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        }
    );
  }

  _gotoLogin(){
    Navigator.push<dynamic>(
        context,
        MaterialPageRoute(
        builder: (BuildContext context) => Login()
    ));
  }

}